package com.lwx.bus.controller;

import com.lwx.bus.domain.Customer;
import com.lwx.bus.service.CustomerService;
import com.lwx.sys.utils.DataGridView;
import com.lwx.sys.utils.ResultObj;
import com.lwx.sys.vo.CustomerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author cn.lwx
 * @since 2020-04-21
 */
@RestController
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @GetMapping("/loadAllCustomer")
    public DataGridView loadAllCustomer(CustomerVo customerVo){
        return customerService.loadAllCustomer(customerVo);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteCustomer")
    public ResultObj deleteCustomer(Integer id){
        try {
            customerService.deleteCustomer(id);
            return ResultObj.DELETE_SUCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/batchDeleteCustomer")
    public ResultObj batchDeleteCustomer(Integer[] ids){
        try {
            customerService.batchDeleteCustomer(ids);
            return ResultObj.DELETE_SUCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 添加
     * @param customer
     * @return
     */
    @PostMapping("addCustomer")
    public ResultObj addCustomer(Customer customer){
        try {
            if (customer.getCustomername() == null){
                return ResultObj.ADD_ERROR;
            }
            customerService.addCustomer(customer);
            return ResultObj.ADD_SUCESS;
        }catch (Exception e){
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 更新
     * @param customer
     * @return
     */
    @PostMapping("/updateCustomer")
    public ResultObj updateCustomer(Customer customer){
        try {
            customerService.updateCustomer(customer);
            return ResultObj.ADD_SUCESS;
        }catch (Exception e){
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/loadCustomerById")
    public DataGridView loadCustomerById(Integer id){
        return new DataGridView(customerService.getById(id));
    }
}

